package com.jinkaimen.kqb.service;

import android.content.Context;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.ht.baselibs.application.HTApplication;
import com.ht.baselibs.bean.BaseGetBean;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.utils.LogUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.constant.ConstantPool;
import com.jinkaimen.kqb.dao.AccountDao;
import com.jinkaimen.kqb.ui.activity.ResultActivity;
import com.jinkaimen.kqb.utils.NotificationUtils;
import com.jinkaimen.kqb.utils.VoiceUtils;

import de.greenrobot.event.EventBus;

public class GetuiService extends GTIntentService {

    public static final String GETUI_CLIEND_ID = "getui_cliend_id";

    public static final int AUTHOR_SUCCESS = 100;
    public static final int AUTHOR_FAIL = 101;

    public static final int GET_MONEY = 400;
    public static final int CHANGE_PWD = 300;
    public static final int NOTIFY = 400;
//    private IWoyouService woyouService;
//    private TipsDialog mTipsDialog;

    @Override
    public void onReceiveServicePid(Context context, int i) {

    }


    @Override
    public void onCreate() {
        super.onCreate();

//        Intent intent = new Intent();
//        intent.setPackage("woyou.aidlservice.jiuiv5");
//        intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
//        startService(intent);//启动打印服务
//        try {
//            bindService(intent, connService, Context.BIND_AUTO_CREATE);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public void onReceiveClientId(final Context context, String clientid) {
        LogUtils.error("clientid:" + clientid + "   推送");
        SharedPreferencesUtils.put(GETUI_CLIEND_ID, clientid);
        Intent downloadIntent = new Intent(context, PushService.class);
        context.startService(downloadIntent);
    }

    @Override
    public void onReceiveMessageData(final Context context, GTTransmitMessage msg) {


        byte[] payload = msg.getPayload();
        if (payload != null) {
            String data = new String(payload);
            LogUtils.error(data + "   推送");
            BaseGetBean baseGetBean = JSON.parseObject(data, BaseGetBean.class);
            Intent intent;
            switch (baseGetBean.resultCode) {
                case AUTHOR_SUCCESS:
                    AccountDao.getInstance().authorStatus(1);
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_AUTHOR_SUCCESS));
                    intent = new Intent(context, ResultActivity.class);
                    intent.putExtra(ResultActivity.RESULT_TITLE, "恭喜您通过审核");
                    intent.putExtra(ResultActivity.RESULT_MSG, baseGetBean.resultMessage);
                    NotificationUtils.show(context, R.mipmap.ic_launcher, HTApplication.getContext().getString(R.string.app_name), baseGetBean.resultMessage, intent, true);
                    break;
                case AUTHOR_FAIL:
                    AccountDao.getInstance().authorStatus(3);
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_AUTHOR_SUCCESS));
//                    intent = new Intent(context, IdentificationShopStep1Activity.class);
//                    intent.putExtra(ResultActivity.RESULT_TITLE, "审核被拒");
//                    intent.putExtra(ResultActivity.RESULT_MSG, baseGetBean.resultMessage);
//                    NotificationUtils.show(context, R.mipmap.ic_launcher, HTApplication.getContext().getString(R.string.app_name), baseGetBean.resultMessage, intent, true);
                    break;
                case CHANGE_PWD:
                    EventBus.getDefault().post(new EventCenter(EventCode.HSY_REQUEST_CODE_UNLOGIN, baseGetBean.resultMessage));
                    break;
                case GET_MONEY:
                    int notificationType = SharedPreferencesUtils.getInt(ConstantPool.NOTIFICATION_TYPE, 0);
                    if (notificationType == 0) {
//                        try {
//                            if (woyouService != null) {
//
//                                woyouService.lineWrap(2, callback);
//                                woyouService.setAlignment(0, callback);
//                                woyouService.printTextWithFont("支付成功", "gh", 54,
//                                        callback);
//                                woyouService.lineWrap(1, callback);
//                                woyouService.printText("--------------------------------", callback);
//                                woyouService.lineWrap(1, callback);
//                                woyouService.printText("" + baseGetBean.resultMessage.split(", ")[0], callback);
//                                woyouService.printText("--------------------------------", callback);
//                                woyouService.lineWrap(3, callback);
//                            }
//                        } catch (RemoteException e) {
//                            e.printStackTrace();
//                        }

                        VoiceUtils.playDetails(baseGetBean.resultMessage.split("，")[0]);
                    } else if (notificationType == 1) {
                        VoiceUtils.play();
                    }
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_BALANCE_CHANGE));
                    EventBus.getDefault().post(new EventCenter(EventCode.EVENT_REFRESH_SELFFRAGMENT_BALANCE));
                    try {
                        intent = new Intent();
                        NotificationUtils.show(context, R.mipmap.ic_launcher, HTApplication.getContext().getString(R.string.app_name), baseGetBean.resultMessage, intent, false);
//                        NotificationUtils.send(context,intent,R.mipmap.ic_launcher,);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
            }
        }
//        String taskid = msg.getTaskId();
//        String messageid = msg.getMessageId();
//        PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean b) {
        if (!b) {
            EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_GETUI_LINE));
        } else {
            EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_GETUI_ONLINE));
        }
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {

    }

//
//    private ServiceConnection connService = new ServiceConnection() {
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//            woyouService = null;
//        }
//
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            woyouService = IWoyouService.Stub.asInterface(service);
//        }
//    };
//
//    ICallback callback = new ICallback.Stub() {
//        @Override
//        public void onRunResult(boolean success) throws RemoteException {
//        }
//
//        @Override
//        public void onReturnString(final String value) throws RemoteException {
//        }
//
//        @Override
//        public void onRaiseException(int code, final String msg)
//                throws RemoteException {
//        }
//    };
}
